package com.ruoyi.project.propertySystem.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.propertySystem.domain.InspectClassify;
import com.ruoyi.project.propertySystem.domain.InspectFormwork;
import com.ruoyi.project.propertySystem.domain.InspectItemRecord;
import com.ruoyi.project.propertySystem.domain.vo.InspectItemRecordVO;
import com.ruoyi.project.propertySystem.domain.vo.InspectRecordVO;
import com.ruoyi.project.propertySystem.mapper.InspectFormworkMapper;
import com.ruoyi.project.propertySystem.mapper.InspectItemRecordMapper;
import com.ruoyi.project.query.InspectRecordQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.ruoyi.project.propertySystem.mapper.InspectRecordMapper;
import com.ruoyi.project.propertySystem.domain.InspectRecord;
import com.ruoyi.project.propertySystem.service.IInspectRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 巡检记录Service业务层处理
 * 
 * @author wangshuo
 * @date 2021-02-14
 */
@Service
public class InspectRecordServiceImpl implements IInspectRecordService 
{
    @Autowired
    private InspectRecordMapper inspectRecordMapper;
    @Autowired
    private InspectItemRecordMapper inspectItemRecordMapper;
    @Autowired
    private InspectFormworkMapper inspectFormworkMapper;

    /**
     * 查询巡检记录
     * 
     * @param id 巡检记录ID
     * @return 巡检记录
     */
    @Override
    public InspectRecord selectInspectRecordById(Long id)
    {
        return inspectRecordMapper.selectInspectRecordById(id);
    }

    /**
     * 查询巡检记录列表
     * 
     * @param inspectRecord 巡检记录
     * @return 巡检记录
     */
    @Override
    public List<InspectRecord> selectInspectRecordList(InspectRecord inspectRecord)
    {
        return inspectRecordMapper.selectInspectRecordList(inspectRecord);
    }

    /**
     * 新增巡检记录
     * 
     * @param inspectRecord 巡检记录
     * @return 结果
     */
    @Override
    public int insertInspectRecord(InspectRecord inspectRecord)
    {
        return inspectRecordMapper.insertInspectRecord(inspectRecord);
    }

    /**
     * 修改巡检记录
     * 
     * @param inspectRecord 巡检记录
     * @return 结果
     */
    @Override
    public int updateInspectRecord(InspectRecord inspectRecord)
    {
        return inspectRecordMapper.updateInspectRecord(inspectRecord);
    }

    /**
     * 批量删除巡检记录
     * 
     * @param ids 需要删除的巡检记录ID
     * @return 结果
     */
    @Override
    public int deleteInspectRecordByIds(Long[] ids)
    {
        return inspectRecordMapper.deleteInspectRecordByIds(ids);
    }

    /**
     * 删除巡检记录信息
     * 
     * @param id 巡检记录ID
     * @return 结果
     */
    @Override
    public int deleteInspectRecordById(Long id)
    {
        return inspectRecordMapper.deleteInspectRecordById(id);
    }

    @Override
    @Transactional
    public List<InspectRecordVO> getUserInspect(Long stationId, Long userId) {
        Long recordId = null;
        InspectRecord userInspect = inspectRecordMapper.getUserInspect(stationId, userId);
        if(userInspect!=null){
            //查询今天的巡检记录
            recordId = userInspect.getId();

//            return inspectItemRecordMapper.selectInspectClassifyByRecordId(recordId);
        }else {

            //获取站点的模板
            List<InspectFormwork> inspectFormworks = inspectFormworkMapper.selectInspectFormworkByStationId(stationId);
            if (inspectFormworks.size() == 0) {
                throw new BaseException("未配置巡检模板，请联系后台管理员");
            }
            InspectRecord inspectRecord = new InspectRecord();
            inspectRecord.setFormworkId(inspectFormworks.get(0).getFormworkId());
            inspectRecord.setStationId(stationId);
            inspectRecord.setStatus(0);
            inspectRecord.setInspectUser(userId);
            inspectRecord.setGmtCreate(new Date());
            inspectRecord.setCreateBy(SecurityUtils.getUsername());
            int i = inspectRecordMapper.insertInspectRecord(inspectRecord);
            //巡检项查询新增
            List<InspectItemRecord> inspectItemRecords = new ArrayList<>();
            inspectFormworks.forEach(x -> {
                InspectItemRecord inspectItemRecord = new InspectItemRecord();
                inspectItemRecord.setInspectRecordId(inspectRecord.getId());
                inspectItemRecord.setClassifyId(x.getClassifyId());
                inspectItemRecord.setItemId(x.getItemId());
                inspectItemRecord.setStatus(0);
                inspectItemRecord.setGmtCreate(new Date());
                inspectItemRecord.setCreateBy(SecurityUtils.getUsername());
                inspectItemRecords.add(inspectItemRecord);
            });
            inspectItemRecordMapper.batchInsert(inspectItemRecords);
            recordId = inspectRecord.getId();
        }
        List<InspectRecordVO> inspectRecordVOS = inspectItemRecordMapper.selectInspectClassifyByRecordId(recordId);
        inspectRecordVOS.forEach(x->{
            //判断所有巡检项是否已经完成
            List<InspectItemRecordVO> inspectItemRecordVOS = inspectItemRecordMapper.getInspectItemsByClassifyAndRecordId(x.getClassifyId(), x.getInspectRecordId());
            x.setItemNum(inspectItemRecordVOS.size());
            int count = (int)inspectItemRecordVOS.stream().filter(y -> y.getStatus() == 0).count();
            if(count>0){
                x.setStatus(0);
            }else {
                x.setStatus(1);
            }


        });
        return inspectRecordVOS;
    }

    @Override
    public PageInfo<InspectRecordVO> getUserInspectRecord(InspectRecordQuery inspectRecordQuery) {
        InspectRecord inspectRecord = new InspectRecord();
        inspectRecord.setInspectUser(SecurityUtils.getLoginUser().getUser().getUserId());
        PageHelper.startPage(inspectRecordQuery.getPageNum(),inspectRecordQuery.getPageSize());
        List<InspectRecordVO> inspectRecords = inspectRecordMapper.getUserInspectRecord(inspectRecord);
        return new PageInfo<>(inspectRecords);
    }
}
